package gui;

import com.google.common.eventbus.Subscribe;
import eventbus.PageEventBus;
import eventbus.events.UpdateConnectStateEvent;
import eventbus.events.UpdatePanelEvent;
import gui.panels.ChatPanel;
import gui.panels.LoginPanel;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;

public class MainPage extends JFrame {
    private static final int WIDTH = 1020;
    private static final int HEIGHT = 651;
    private static final int LOCATION_X = 500;
    private static final int LOCATION_Y = 200;

    JLabel connectStateLabel = new JLabel("链接状态:");
    JLabel connectStateInput = new JLabel("正在链接中");

    BasePanel curPanel;

    public MainPage() {
        init();
        setTitle("主页面");
        setLayout(null);
        PageEventBus.subscribe(this);
        curPanel = PanelFactory.getPanel(LoginPanel.class);
        connectStateLabel.setBounds(750, 550, 60, 30);
        connectStateInput.setBounds(810, 550, 100, 30);
        connectStateInput.setForeground(Color.ORANGE);
        this.add(connectStateLabel);
        this.add(connectStateInput);
        this.add(curPanel);
        this.setBounds(LOCATION_X, LOCATION_Y, WIDTH, HEIGHT);
        this.setResizable(false);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("关闭");
                System.exit(0);
            }
        });
    }


    @Subscribe
    public void updatePanel(UpdatePanelEvent event) {
        this.remove(curPanel);
        curPanel = PanelFactory.getPanel(event.getPanelType());
        this.add(curPanel);
        repaint();
    }

    @Subscribe
    void updateConnectState(UpdateConnectStateEvent event) {
        String state = event.isConnect() ? "已连接" : "正在链接中";
        Color color = event.isConnect() ? Color.green : Color.ORANGE;
        connectStateInput.setText(state);
        connectStateInput.setForeground(color);
    }

    void init() {
        String themeName = "Nimbus";
        String fontName = "微软雅黑";
        try {
            //更改全局字体
            FontUIResource fontRes = new FontUIResource(new FontUIResource(fontName, Font.PLAIN, 14));
            for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements(); ) {
                Object key = keys.nextElement();
                Object value = UIManager.get(key);
                if (value instanceof FontUIResource) {
                    UIManager.put(key, fontRes);
                }
            }
            //更改风格更好看一些
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if (themeName.equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
